﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;
using Customer_Profile.Admin;

namespace Customer_Profile.eCard.Settle
{
    public partial class needSettle_search : System.Web.UI.Page
    {
        string strUserName = "";
        string strUserID = "";
        string strEntityID = "";
        string strEntity = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";

            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntityID = ((UserInfoEntity)Session["usermodel"]).UserEntityID;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }

            if (!Page.IsPostBack)
            {
                //Comm.BindDll(ref dropTerm, "TermType", true, "");
                DataSet dsProd = ProductDao.getAllProduct("ALL");
                dropProd.DataSource = dsProd;
                dropProd.DataTextField = "procduct_name";
                dropProd.DataValueField = "procduct_code";
                dropProd.DataBind();
                ListItem liAll = new ListItem();
                liAll.Text = "All";
                liAll.Value = "All";
                dropProd.Items.Insert(0, liAll);

                //Comm.BindDll(ref dropEntity, "Entity", true, "");
                DataSet dsEntity = EntityDao.getEntityList("N");
                dropEntity.DataSource = dsEntity;
                dropEntity.DataTextField = "entity_name";
                dropEntity.DataValueField = "entity_id";
                dropEntity.DataBind();
                //ListItem liAllEntity = new ListItem();
                //liAllEntity.Text = "All";
                //liAllEntity.Value = "All";
                //dropEntity.Items.Insert(0, liAllEntity);
                dropEntity.Items.FindByValue(strEntityID).Selected = true;
                dropEntity.Enabled = false;

                txtcreatedatebegin.Text = DateTime.Now.ToString("yyyy-MM-dd");
                txtcreatedateend.Text = DateTime.Now.AddDays(10).ToString("yyyy-MM-dd");
            }
            bindGrid();
        }

        private void bindGrid()
        {
            try
            {
                string strWhere = " where d.entity_id='" + strEntityID + "' and d.status='PASS' AND if_locked='N' AND if_returncard='N' AND deal_balance>0 ";
                if (txtCardNo.Text.Trim() != "")
                    strWhere += " and d.card_no ='" + txtCardNo.Text.Trim() + "'";
                if (txtCpartyName.Text.Trim() != "")
                    strWhere += " and c.cparty_name like '%" + txtCpartyName.Text.Trim() + "%'";
                //if (dropEntity.SelectedIndex > 0)
                //    strWhere += " and d.entity = '" + dropEntity.SelectedItem.Text + "'";
                //dropProd
                if (dropProd.SelectedIndex > 0)
                {
                    strWhere += " and d.prod_code = '" + dropProd.SelectedItem.Value + "'";
                }
                if (txtcreatedatebegin.Text != "" && txtcreatedateend.Text != "")
                    strWhere += " and convert(date,d.due_date) between '" + txtcreatedatebegin.Text.Trim() + "' and '" + txtcreatedateend.Text.Trim() + "'";

                DataSet ds = DealDao.getCardDealsbysome(strWhere); //.getCardDealsneedSettle(strWhere);
                if (ds != null && ds.Tables.Count > 0)
                {
                    GvData.DataSource = ds.Tables[0];
                    GvData.DataBind();
                }
                else
                {
                    GvData.DataBind();
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("bindGrid", ex.ToString());
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void GvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GvData.PageIndex = e.NewPageIndex;
            bindGrid();
        }

        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lb = (LinkButton)e.Row.Cells[1].Controls[1];
                lb.Attributes.Add("onclick", "javascript:window.open('/eCard/CardDetail.aspx?cardno=" + lb.Text + "');return false");
                Label lbdealno = (Label)e.Row.Cells[0].Controls[1];

                string strStatus = e.Row.Cells[4].Text;
                string if_isExtension = e.Row.Cells[11].Text;

                LinkButton lbAction = (LinkButton)e.Row.Cells[12].Controls[1];

                //添加“申请结算”连接
                lbAction.Text = "申请结算";
                lbAction.Attributes.Add("onclick", "javascript:window.location.href='settle_application.aspx?deal_no=" + lbdealno.Text + "';return false");
            }
        }
    }
}